'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate','shiftform', 'validate.en', 'koala-search'],
function($ ) {
  return {
    
    init: function() {
      this.initPage();
      this.initSearch();
    },
    
    initPage: function() {
      
        $(document).on("shown.bs.modal" , "#create" ,function(e){
            
        	$(".firstSalary").hide();
            $(document).on("change" , "select[name='organization'] , select[name='driverCount']" ,function(e){
                
                if($("#searchOrganization").length > 0){
                    var orgId = $('select[name="organization"]').val().trim();
                }
                else{
                    var orgId = $('input[name="organization"]').val();
                }
                
                var driverCount = $('select[name="driverCount"]').val().trim();
                
                if((orgId != "" && orgId != undefined && orgId != null) && (driverCount != "" && driverCount != undefined && driverCount != null)) {
                       
                       $.ajax({
                           url:window.__CTX__.root + "/driver-salaries?prepare=true&orgId="+orgId + "&driverCount="+driverCount,
                           type:"GET",
                       }).done(function(data){
                           data = typeof data === 'String' ? JSON.parse(data) : data
                                   
                           if(parseInt(data.code) == 0) {  
                               
                               var response = data.data;
                               
                               if(response.driverSalary != null) {
                            	   
                                   $('input[name="mainDriverFullSalary"]').val(response.driverSalary.mainDriverFullSalary)
                                   $('input[name="mainDriverEmptySalary"]').val(response.driverSalary.mainDriverEmptySalary)
                                   
                                   if(driverCount>1){
                                       $(".firstSalary").show();
                                       $('input[name="firstViceDriverFullSalary"]').val(response.driverSalary.firstViceDriverFullSalary)
                                       $('input[name="firstViceDriverEmptySalary"]').val(response.driverSalary.firstViceDriverEmptySalary)
                                   }
                                   else {
                                	   $(".firstSalary").hide();
                                       $('input[name="firstViceDriverFullSalary"]').val(0.0)
                                       $('input[name="firstViceDriverEmptySalary"]').val(0.0)
                                   }
                                  
                               } else {
                                   $('input[name="mainDriverFullSalary"]').val(0.0)
                                   $('input[name="mainDriverEmptySalary"]').val(0.0)
                                   $('input[name="firstViceDriverFullSalary"]').val(0.0)
                                   $('input[name="firstViceDriverEmptySalary"]').val(0.0)
                               }

                           } else {
                               console.log(data.code);
//                               changeBackdropMessage('查询失败', 'text-danger', options.failure, data.data.url)
                           }
                           
                       }).fail(function() {
                           console.log(data.code);
//                         changeBackdropMessage('查询失败', 'text-danger', data.url)
                       })
                }
                else{
                $('input[name="mainDriverFullSalary"]').val(0.0)
                    $('input[name="mainDriverEmptySalary"]').val(0.0)
                    $('input[name="firstViceDriverFullSalary"]').val(0.0)
                    $('input[name="firstViceDriverEmptySalary"]').val(0.0)
                }
            })
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
            
        });
    },
    
    initSearch: function() {
        
        $(document).on("shown.bs.modal" , "#search" ,function(e){
        
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
        });
    }
    
  }
})